#!/usr/bin/php
<?php   
error_reporting(E_ALL);
include "model/loadres_model.php";
//确保在连接客户端时不会超时   
set_time_limit(0);   
  
//设置IP和端口号   
$address='10.232.128.175';   
$port=12347;    //调试的时候，可以多换端口来测试程序！   
  
//创建一个SOCKET   
if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)   
{   
echo "socket_create() 失败的原因是:".socket_strerror($sock)."<br>";   
}   
  
//绑定到socket端口   
if(($ret=socket_bind($sock,$address,$port))<0)   
{   
echo "socket_bind() 失败的原因是:".socket_strerror($ret)."<br>";   
}   
  
//开始监听   
if(($ret=socket_listen($sock,20))<0)   
{   
echo "socket_listen() 失败的原因是:".socket_strerror($ret)."<br>";   
}   
  
do {   
if (($msgsock = socket_accept($sock)) < 0)   
{   
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";   
break;   
}   
//发到客户端   
$msg="connect ok!\n";
socket_write($msgsock, $msg, strlen($msg));   
  
echo "读取客户端发来的信息\n";   
$buf = socket_read($msgsock,8192);   
echo date("Y-m-d_H:i:s")." [info]: $buf\n";
$bufs = explode("\x01", $buf);
switch($bufs[0])
{
case '1':
	recordres($bufs);
	break;
case '2':
	updatefile($bufs);
	break;
case '3':
	recordupres($bufs);
	break;
default:
	break;
}
echo "$buf\n";
socket_close($msgsock);   
} while (true);   
  
socket_close($sock);   

function updatefile($bufs)
{
	$farr = explode("\x02", $bufs[1]);
	$fo = fopen('data/loaddatafiles', "w+");
	foreach($farr as $fn)
	{
		fwrite($fo, $fn."\n");
	}
	fclose($fo);
}
function recordupres($bufs)
{
	$buffds = explode("\x02", $bufs[1]);
	if(count($buffds)>=3)
	{
		$id = explode("\x03", $buffds['0']);
		$id = trim($id[1]);
	
		$runtime = explode("\x03", $buffds['1']);
		$runtime = trim($runtime[1]);
		$runtime = substr($runtime,0,4).'-'.substr($runtime,4,2).'-'.substr($runtime,6,2).' '.substr($runtime,8,2).':'.substr($runtime,10,2);
	
		$detail = $buffds['2'];
		if(trim($detail)==null or trim($detail)=="")
			$detail = 'null';
		
		$its = explode("<br />", $detail);
		//array_shift($its);
		$itt = explode("\t", $its[1]);
		$qps = $itt[1];
		echo "-------------".$itt[1]."--------------";
		if($itt[1]==null or $itt[1]=="")
		{
			$qps = 0;
		}
		$rt = 0;
		$to = 'null';
		
		$sql ='insert into loadresults (`scenarioid`,`runtime`,`detail`,`rt`,`timeoutrate`,`qps`) values ('.$id.',\''.$runtime.'\',\''.$detail.'\','.$rt.',\''.$to.'\','.$qps.')';
		echo $sql."\n";
		mysql_query($sql) or die(mysql_error());	
	}
	else
	{
		echo "--------".count($buffds)."-------------\n";
	}
	
	
}

function recordres($bufs)
{
	$buffds = explode("\x02", $bufs[1]);
	echo "\n".count($buffds)."\n";
	foreach($buffds as $bt)
	{
		error_log($bt);
	}
	if(count($buffds)==7 && in_array('', $buffds) == false && in_array(null, $buffds) == false)
	{
		error_log('ok');
		$id = explode("\x03", $buffds['0']);
		$id = trim($id[1]);

		$taskid = explode("\x03", $buffds['1']);
		$taskid = trim($taskid[1]);
	
		$runtime = explode("\x03", $buffds['2']);
		$runtime = trim($runtime[1]);
		$runtime = substr($runtime,0,4).'-'.substr($runtime,4,2).'-'.substr($runtime,6,2).' '.substr($runtime,8,2).':'.substr($runtime,10,2);
	
		$detail = explode("\x03", $buffds['3']);
		$detail = trim($detail[1]);
		if(trim($detail[1])==null or trim($detail[1])=="")
			$detail = 'null';
		
		//$detail = implode(",", $detail);
	
		$qps = explode("\x03", $buffds['4']);
		if(trim($qps[1])==null or trim($qps[1])=="") 
			$qps = 0;
	
		$rt = explode("\x03", $buffds['5']);
		if(trim($rt[1])==null or trim($rt[1])=="") 
			$rt = 0;
	
		$to = explode("\x03", $buffds['6']);
		if(trim($to[1])==null or trim($to[1]==""))
			$to = 0;
		$sql ='insert into loadresults (`scenarioid`,`taskid`,`runtime`,`detail`,`rt`,`timeoutrate`,`qps`) values ('.$id.','.$taskid.',\''.$runtime.'\',\''.$detail.'\','.$rt[1].',\''.$to[1].'\','.$qps[1].')';
		
		error_log($sql);
		mysql_query($sql) or die(mysql_error());	
	}
	else
	{	
		echo "Error!\n";
	}

}

?>  
